package com.chart.web.controller.common.ai;

import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ColorUtils;
import com.chart.web.controller.chart.dto.ChartColorDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/color")
public class ColorController {

    @PostMapping("/generate")
    public AjaxResult queryColors(@RequestBody ChartColorDTO chartColorDTO) {
        List<String> baseColors = chartColorDTO.getColorCodes();
        Integer totalColors = chartColorDTO.getTotalColors();

        // 使用日志打印 baseColors 中的每个颜色
        for (String baseColor : baseColors) {
            log.info("Base color: {}", baseColor);
        }

        // 检查 baseColors 是否为空或 null
        if (baseColors == null || baseColors.isEmpty()) {
            log.warn("输入的颜色列表为空或null");
            return new AjaxResult(603, "颜色为空，请重新上传！");
        }

        // 检查 totalColors 是否为 null
        if (totalColors == null) {
            log.warn("颜色数量为空");
            return new AjaxResult(603, "颜色数量为空，请重新上传！");
        }

        try {
            // 调用生成渐变色的方法
            List<String> generatedColors = ColorUtils.generateGradientColors(baseColors, totalColors);
            return AjaxResult.success(generatedColors);
        } catch (Exception e) {
            log.warn("颜色生成算法异常: {}", e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}
